package com.bwie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.pojo.TbBrand;
import com.bwie.pojo.TbType;
import com.bwie.service.TbBrandService;
import com.bwie.mapper.TbBrandMapper;
import com.bwie.utils.R;
import com.bwie.vo.PageInfoVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
* @author 31803
* @description 针对表【tb_brand(品牌表)】的数据库操作Service实现
* @createDate 2025-07-19 08:36:29
*/
@Service
public class TbBrandServiceImpl extends ServiceImpl<TbBrandMapper, TbBrand>
    implements TbBrandService{

    @Autowired
    TbBrandMapper tbBrandMapper;



    @Override
    public R getBranPage(PageInfoVo pageInfoVo) {
        //构造分页
        Page<TbBrand> page = new Page<>(pageInfoVo.getPageNum(), pageInfoVo.getPageSize());
        //分页查询数据
        QueryWrapper<TbBrand> wrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(pageInfoVo.getKeyWord())){
            wrapper.lambda().like(TbBrand::getBrandName,pageInfoVo.getKeyWord());
        }
        wrapper.lambda().orderByDesc(TbBrand::getCreateTime);
        Page<TbBrand> tbBrandPage = tbBrandMapper.selectPage(page, wrapper);

        return R.OK(tbBrandPage);


    }

    @Override
    public R getAddBrand(TbBrand tbBrand) {

      //校验品牌名称是否重复
        TbBrand tbBrand1 = tbBrandMapper.selectOne(
                new QueryWrapper<TbBrand>().lambda().eq(TbBrand::getBrandName, tbBrand.getBrandName())
        );
        if(tbBrand1!=null){
            return R.ERROR(500,"品牌名称重复");
        }

        //添加审计字段
        tbBrand.setCreateTime(new Date());
        tbBrand.setCreateBy("管理员");
      tbBrandMapper.insert(tbBrand);


        return R.OK();
    }

    @Override
    public R updateStatus(TbBrand tbBrand) {
        //查看品牌是否存在
        TbBrand tbBrand1 = tbBrandMapper.selectById(tbBrand);
        if(tbBrand1==null){
            return R.ERROR(500,"商品品牌不存在");
        }
        tbBrand1.setBrandStatus(tbBrand.getBrandStatus());
        tbBrandMapper.updateById(tbBrand1);
        return R.OK();
    }
}




